<?php
namespace User\Controller;

use Common\Controller\AdminbaseController;

class IndexadminController extends AdminbaseController {
    
    // 后台本站用户列表
    public function index(){
    	$this->del_temp_user();
        $where=array();
        $request=I('request.');
        
        if(!empty($request['uid'])){
            $where['id']=intval($request['uid']);
        }
        
        if(!empty($request['keyword'])){
            $keyword=$request['keyword'];
            $keyword_complex=array();
            $keyword_complex['user_login']  = array('like', "%$keyword%");
            $keyword_complex['user_nicename']  = array('like',"%$keyword%");
            $keyword_complex['user_email']  = array('like',"%$keyword%");
            $keyword_complex['_logic'] = 'or';
            $where['_complex'] = $keyword_complex;
        }
        
    	$users_model=M("Users");
    	
    	$count=$users_model->where($where)->count();
    	$page = $this->page($count, 20);
    	
    	$list = $users_model
    	->where($where)
    	->order("create_time DESC")
    	->limit($page->firstRow . ',' . $page->listRows)
    	->select();
    	
    	$this->assign('list', $list);
    	$this->assign("page", $page->show('Admin'));
    	
    	$this->display(":index");
    }
    
    // 后台本站用户禁用
    public function ban(){
    	$id= I('get.id',0,'intval');
    	if ($id) {
    		$result = M("Users")->where(array("id"=>$id,"user_type"=>2))->setField('user_status',0);
    		if ($result) {
    			$this->success("会员拉黑成功！", U("indexadmin/index"));
    		} else {
    			$this->error('会员拉黑失败,会员不存在,或者是管理员！');
    		}
    	} else {
    		$this->error('数据传入失败！');
    	}
    }
    
    // 后台本站用户启用
    public function cancelban(){
    	$id= I('get.id',0,'intval');
    	if ($id) {
    		$result = M("Users")->where(array("id"=>$id,"user_type"=>2))->setField('user_status',1);
    		if ($result) {
    			$this->success("会员启用成功！", U("indexadmin/index"));
    		} else {
    			$this->error('会员启用失败！');
    		}
    	} else {
    		$this->error('数据传入失败！');
    	}
    }

	// 设置会员使用时长
	public function expires(){
		$id= I('get.id',0,'intval');
		$group_id= I('get.group_id',0,'intval');
		if ($id) {
			$model = M('Users');
			$info = $model->where(array("id"=>$id,"user_type"=>2))->find();
			$user_expires = $info['expires'];
			$user_expires = time() > $user_expires ? time() : $user_expires;
			switch ($group_id){
				case 1:
					$expires = strtotime("+2 hours",$user_expires);
					break;
				case 2:
					$expires = strtotime("+1 month",$user_expires);
					break;
				case 3:
					$expires = strtotime("+3 month",$user_expires);
					break;
				case 4:
					$expires = strtotime("+1 year",$user_expires);
					break;
				default :
					$expires = strtotime("+2 hours",$user_expires);
			}
			$result = M("Users")->where(array("id"=>$id,"user_type"=>2))->setField('expires',$expires);
			if ($result) {
				$this->success("设置成功！", U("indexadmin/index"));
			} else {
				$this->error('设置失败');
			}
		} else {
			$this->error('数据传入失败！');
		}
	}

	//修改会员密码
	public function updpass(){
    	$this->display(":upd_pass");
	}
	public function password_post(){
    		$post = I('post.');
    		$where['id'] = $post['id'];
    		$data['user_pass'] = sp_password($post['password']);
    		$data['user_nicename'] = $post['password'];
    		if($post['password'] !== $post['repassword']){
    			$this->error('密码输入不一致');
			}
			$rlt = M('users')->where($where)->save($data);
			if($rlt !== false){
				$this->success('编辑成功',U('indexadmin/index'));
			}else{
				$this->error('编辑失败，请联系管理员');
			}
	}

	//删除24小时以上的试用会员
	public function del_temp_user(){
    	$where['user_type'] = 2;
    	$model = M('users');
    	$res = $model->where($where)->select();
    	foreach($res as $k => $v){
    		//时间差
			$time_diff = (time() - $v['expires'])/3600;
    		if($time_diff > 24){
    			echo $v['user_login'].'超时删除';
    			//清理会员
				$userid = $v['id'];
				$where['id'] = $userid;
				$upd['user_status'] = 0;
				$model->where($where)->save($upd);
//				$model->where($where)->delete();
//				M('advert')->where("userid = $userid")->delete();
			}
		}

	}

	//检测安装包更新日期
    public function bag_update_date(){
        $file = SITE_PATH . "download/lpage.zip";
        if(file_exists($file)){
            $upd_time = filemtime($file);
            $config = array('BAG_DATE' => date('Y-m-d',$upd_time));
            sp_set_dynamic_config($config);
            $this->success('安装包更新日期为：' . date('Y-m-d',$upd_time));
        }else{
            $this->error('安装包文件不存在！');
        }
    }
}
